Use case: Take/Release a semaphore

This use case shows how the application take a semaphore before accessing a shared resource, then releases it once the processing is completed.

Sequence diagram

HAL1

HAL2

Take/Release a semaphore

@startuml
participant "User Application" as App
participant "HSEM Driver" as HSEM

group Take the hardware semaphore
App -> HSEM: HAL_HSEM_Take(HSEM_ID_0, process_id)
end
group Check semaphore state after take
App -> HSEM: HAL_HSEM_IsSemTaken(HSEM_ID_0)
HSEM --> App: 1U
note right of HSEM
The hardware semaphore is taken
end note
end

group Release the hardware semaphore
App -> HSEM: HAL_HSEM_Release(HSEM_ID_0, process_id)
end

group Check semaphore state after release
App -> HSEM: HAL_HSEM_IsSemTaken(HSEM_ID_0)
HSEM --> App: 0U
note right of HSEM
The hardware semaphore is released
end note
end
@enduml

Take/Release a semaphore

@startuml
participant "User Application" as App
participant "HSEM Driver" as HSEM

group Take the hardware semaphore
App -> HSEM: HAL_HSEM_Take(HAL_HSEM_0, process_id)
end
group Check semaphore state after take
App -> HSEM: HAL_HSEM_IsTaken(HAL_HSEM_0)
HSEM --> App: HAL_HSEM_TAKEN
note right of HSEM
The hardware semaphore is taken
end note
end

group Release the hardware semaphore
App -> HSEM: HAL_HSEM_Release(HAL_HSEM_0, process_id)
end

group Check semaphore state after release
App -> HSEM: HAL_HSEM_IsTaken(HAL_HSEM_0)
HSEM --> App: HAL_HSEM_RELEASED
note right of HSEM
The hardware semaphore is released
end note
end


@enduml

Code snippet

HAL1

HAL2

uint32_t sem_state;
#define HSEM_SEM_ID      (0U) /* HW semaphore 0*/

/* HSEM Clock Enable */
__HAL_RCC_HSEM_CLK_ENABLE() ;

HAL_HSEM_Take(HSEM_SEM_ID, 1U);

sem_state = HAL_HSEM_IsSemTaken(HSEM_SEM_ID);

if (sem_state == 1U)
{
   /* The hardware semaphore is taken */
}

HAL_HSEM_Release(HSEM_SEM_ID, 1U);

sem_state = HAL_HSEM_IsSemTaken(HSEM_SEM_ID);

if (sem_state == 0U)
{
   /* The hardware semaphore is released */
}
hal_hsem_status_t sem_state;


/* HSEM Clock Enable */
HAL_RCC_HSEM_EnableClock();

HAL_HSEM_Take(HAL_HSEM_0, 1U);

sem_state = HAL_HSEM_IsTaken(HAL_HSEM_0);

if (sem_state == HAL_HSEM_TAKEN)
{
      /* The hardware semaphore is taken */
}

HAL_HSEM_Release(HAL_HSEM_0, 1U);

sem_state = HAL_HSEM_IsTaken(HAL_HSEM_0);

if (sem_state == HAL_HSEM_RELEASED)
{
      /* The hardware semaphore is released */
}